﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    /// <summary>
    /// 业务账单操作
    /// </summary>
    public class BillOptController : ApiController
    {
        /// <summary>
        /// 获取账单汇总列表
        /// eg.: 0000000001|TST1|WEB|20|1|0||0|R|3,4,5|
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|账单id（0=所有）|制单人id（空值=所有）|客户id（0=所有）|收付标志（R-收；P-付；空值=所有）|账单pid（英文逗号分隔，空值=所有）|预结标志（Y-已全部做账【做账：应收应付金额=预结金额】；N-未全部做账；空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/billheaderopt")]
        public RespBill GetBizBill(string key)
        {
            RespBill resp = new RespBill();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[6];
            string strRplb = strsKeyOriginal[8].ToUpper();
            string[] strsPid = strsKeyOriginal[9].Split(',');
            string strSettleLb = strsKeyOriginal[10].ToUpper();
            int iPageSize, iCurrPage, iBillid, iCusid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iBillid) || !int.TryParse(strsKeyOriginal[7], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetBillExtInfo(strCreateid, strComcd, iPageSize, iCurrPage, iBillid, iCusid, strRplb, strsPid, GlobalSettings.FNTP_BIZ, strSettleLb);
            return resp;
        }


        /// <summary>
        /// 维护账单汇总列表
        /// </summary>
        /// <param name="_reqInfo">基本信息更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/billheaderopt")]
        public RespBill UpdateBizBillHeader(ReqBillBase _reqInfo)
        {
            RespBill resp = new RespBill();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateBillBaseInfo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.billbaseinfo, GlobalSettings.FNTP_BIZ);
            return resp;
        }

        /// <summary>
        /// 账单提交审批
        /// </summary>
        /// <param name="_reqInfo">敏感信息更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/submitbill")]
        public RespFinaNullResultInfo SubmitBillCore(ReqBillSens _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.SubmitBill(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.billcore);
            return resp;
        }

        /// <summary>
        /// 增删账单明细
        /// </summary>
        /// <param name="_reqInfo">账单明细调整请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/adjustbill")]
        public RespFinaNullResultInfo AdjustBillDetail(ReqAdjustBillDetail _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.AdjustBizBillDetail(_reqInfo.billid, _reqInfo.optlb, _reqInfo.feelist);
            return resp;
        }

        /// <summary>
        /// 账单审批流程
        /// eg.: 0000000001|TST1|WEB|5
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|账单id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/billrevflow")]
        public RespBillRevFlow GetBillRevFlow(string key)
        {
            RespBillRevFlow resp = new RespBillRevFlow();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iBillid;
            if (!int.TryParse(strsKeyOriginal[3], out iBillid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iBillid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。账单流水号是必需的";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetBillRevFlow(iBillid, GlobalSettings.APPLYTP_BILL);
            return resp;
        }

        /// <summary>
        /// 根据账单获取项目明细信息
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|账单id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/projdtlfee")]
        public RespProjDtlFee GetProjDtlFee(string key)
        {
            RespProjDtlFee resp = new RespProjDtlFee();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iBillid;
            if (!int.TryParse(strsKeyOriginal[3], out iBillid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iBillid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。账单流水号是必需的";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetProjDtlFee(iBillid);
            return resp;
        }


    }



}
